﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using Amazon.PAAPI.WCF;
using Amazon.PAAPI;
using System.Net;


namespace Amazon.PAAPI
{
    class Program
    {
        private const string accessKeyId = "AKIAI66WSGSDASQTRQ3A";
        private const string secretKey = "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";
        
        
        static void Main(string[] args)
        {
            // Instantiate Amazon ProductAdvertisingAPI client
            
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            binding.MaxReceivedMessageSize = int.MaxValue;

            AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient(
                        binding,
                        new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

            // add authentication to the ECS client
            amazonClient.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));

            // prepare an ItemSearch request
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "Books";
            request.Title = "Eliade";            
            request.ResponseGroup = new string[] { "Small" };

            ItemSearch itemSearch = new ItemSearch();
            itemSearch.AssociateTag = ConfigurationManager.AppSettings["associateTag"]; 
            itemSearch.Request = new ItemSearchRequest[] { request };
            itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
            itemSearch.AssociateTag = "cotrobaiero-20";

            // send the ItemSearch request
            ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);

            // write out the results from the ItemSearch request
            foreach (var item in response.Items[0].Item)
            {
                if (item.ItemAttributes.Author != null)
                    foreach (var a in item.ItemAttributes.Author)
                        Console.WriteLine("Author: " + a);
                Console.WriteLine("Manufacturer: " + item.ItemAttributes.Manufacturer);
                Console.WriteLine("ProductGroup: " + item.ItemAttributes.ProductGroup);
                Console.WriteLine("Title: " + item.ItemAttributes.Title);

                /*string html = new WebClient().DownloadString(item.DetailPageURL);
                string description = String.Empty;
                string details = String.Empty;
                try
                {
                    string keyWord1 = "<div class=\"productDescriptionWrapper\">";
                    string keyWord2 = "</div>";
                    description = Grab(html, keyWord1, keyWord2);
                }
                catch { }
                try
                {
                    string keyWord1 = "<h2>Product Details</h2>";
                    string keyWord2 = "<li><b>Text-to-Speech";
                    details = Grab(html, keyWord1, keyWord2);
                }
                catch { }

                Console.WriteLine("Description: " + description.Substring(0, description.Length > 300 ? 300 : description.Length));
                Console.WriteLine("Details: " + details.Substring(0, details.Length > 300 ? 300 : details.Length));*/
                Console.WriteLine();
            }
            //11 entries in 24 secunde daca las description si details
            //11 in 7 secunde daca nu fac parsare de html               =>2 secunde in plus/item daca fac si parsarea html
            Console.WriteLine("done...enter any key to continue>");
            Console.ReadLine();
        }
        public static string Grab(string html,string keyWord1, string keyWord2)
        {
            string rez = html.Substring(html.IndexOf(keyWord1) + keyWord1.Length, html.IndexOf(keyWord2, html.IndexOf(keyWord1) + 10) - html.IndexOf(keyWord1) - keyWord1.Length);
            while (rez.Contains("<script"))
            { 
                int index1 = rez.IndexOf("<script"); int index2 = rez.IndexOf("</script>", index1);
                rez = rez.Replace(rez.Substring(index1, index2 - index1), " ");
            }
            return rez;
        }
    }
}
